Python project template - pre-commit
Table of Content
Python project template - pre-commit#
Demo of usage pre-commit tool
Install pre-commit tool and config it with four tools
- isort
- black
- mypy
- flake8
pre-commit#
- Install pre-commit
- Add config file
- Install hooks
- Add hooks
- Run
install
python -m pip install pre-commit
config
touch .pre-commit-config.yaml
install hook
pre-commit install
hooks#
isort#
isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type.
- config pre-commit
- add isort config to
pyproject.toml - install and run
.pre-commit-config.yaml
repos:
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
pyproject.toml
[settings]
[tool.black]
line-length = 120
[tool.isort]
profile = "black"
install and run
pre-commit install
# Run
pre-commit run -a
config with black
config isort with black profile
black#
Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in place.
- config pre-commit
- add isort config to
pyproject.toml - install and run
.pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
language_version: python3.10
pyproject.toml
[settings]
[tool.black]
line-length = 120
[tool.isort]
profile = "black"
install and run
pre-commit install
# Run
pre-commit run -a
config isort
config isort with black profile
black and isort format import different, if both config as pre-commit action they interrupt each other
disable fmt format for code section
# fmt: off
...
# fmt: on
mypy#
- config pre-commit
- add mypy section to
pyproject.tomlconfig - install and run
.pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.942
hooks:
- id: mypy
pyproject.toml
[settings]
[tool.black]
line-length = 120
[tool.mypy]
add inline ignore
# type: ignore
flake8#
- config pre-commit
- add
.flake8config
code style checker for PEP8
.pre-commit-config.yaml
repos:
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
.flake8
[flake8]
max-line-length = 120
max-complexity = 10
exclude=src/apm_demos/test/*
Recap#
- isort, black and mypy config from
myproject.toml - flake8 config with its on file
.flake8 - isort must be config with black profile
disabled pre-commit
git commit --no-verify
.pre-commit-config.yaml#
files: src/apm_demos
repos:
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
language_version: python3.10
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.942
hooks:
- id: mypy
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8